package coms.dataaccess.hibernate;

import coms.dataaccess.dao.ReviewDAO;
import coms.dto.Conference;
import coms.dto.Review;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class ReviewDAOImpl extends GenericDAOImpl<Review, Integer> implements ReviewDAO {

    private Session session;

    @Override
    public List<Review> search(Review review) throws SQLException {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Review.class, "review");
        if (review == null) {
            return criteria.list();
        }

        if (review.getPublication() != null) {
            criteria.createAlias("review.publication", "pub")
                    .add(Restrictions.eq("pub.id", review.getPublication().getId()));
        }

        if (review.getReviewer() != null) {
            criteria.createAlias("review.reviewer", "rev")
                    .add(Restrictions.eq("rev.id", review.getReviewer().getId()));
        }

         List<Review> reviewList = criteria.list();
        clear();
        return reviewList;
    }
}
